package com.sands.network.helper;


import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.sands.app.App;
import com.sands.network.api.ApiTokenAuthService;
import com.sands.network.api.BaseNormalApiService;
import com.sands.network.api.BaseTokenApiService;
import com.sands.network.api.BaseUpdataServiceApi;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.sands.network.util.Config.BaseUrl;

/**
 * Created by kunqi.lai on 2016/8/13 18:00.
 * email :781548819@qq.com
 * Retrofit帮助类
 */
public class RetrofitHelper

{

    private static OkHttpClient mOkHttpClient;

    private static final String API_BASE_URL = "http://v.juhe.cn/weixin/query?key=6363b8fbeed7ad0c89cce469b386a0ff";


    static {
        initOkHttpClient();
    }

    /**
     * token过期拦截器拦截code = 401重新请求token数据调用
     *
     * @return
     */
    public static ApiTokenAuthService refreshToken() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(mOkHttpClient).build();
        return retrofit.create(ApiTokenAuthService.class);
    }




    /**
     * 需要使用token的接口调用
     *
     * @return
     */
    public static BaseTokenApiService callBaseTokenApiAccess() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .client(mOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        return retrofit.create(BaseTokenApiService.class);
    }





    //上传接口
    public static BaseUpdataServiceApi BaseUpdataServiceApi() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BaseUrl)
                .client(mOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        return retrofit.create(BaseUpdataServiceApi.class);
    }

    /**
     * 不带token的请求
     *
     * @return
     */
    public static BaseNormalApiService callBaseNormalApiAccess() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .client(mOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit.create(BaseNormalApiService.class);
    }

    /**
     * 初始化OKHttpClient
     * 设置缓存
     * 设置超时时间
     * 设置打印日志
     * 设置UA拦截器
     */
    private static void initOkHttpClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        if (mOkHttpClient == null) {
            synchronized (RetrofitHelper.class) {
                if (mOkHttpClient == null) {
                    //设置Http缓存
                    Cache cache = new Cache(new File(App.getInstance()
                            .getCacheDir(), "HttpCache"), 1024 * 1024 * 100);
                    mOkHttpClient = new OkHttpClient.Builder()
                            .cache(cache)
                            .addInterceptor(interceptor)
                            .addInterceptor(new TokenInterceptor())
                            .addNetworkInterceptor(new StethoInterceptor())
                            .retryOnConnectionFailure(true)
                            .connectTimeout(30, TimeUnit.SECONDS)
                            .writeTimeout(20, TimeUnit.SECONDS)
                            .readTimeout(20, TimeUnit.SECONDS)
                            .build();
                }
            }
        }
    }


}
